package Java7_Concurrency_Cookbook.concurrent_custom.custom_lock;

import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.AbstractQueuedSynchronizer;

/**
 * AbstractQueuedSynchronizer, 它提供操作来对临界区代码的访问进行控制,
 * 并对等待访问临界区代码的阻塞线程队列进行管理.<br/>
 *
 * @author lixiangke
 * @date 2017/11/28
 */
public class MyAbstractQueuedSynchronizer extends AbstractQueuedSynchronizer {
    private AtomicInteger state;

    public MyAbstractQueuedSynchronizer() {
        this.state = new AtomicInteger(0);
    }

    @Override
    protected boolean tryAcquire(int arg) {
        return state.compareAndSet(0, 1);
    }

    @Override
    protected boolean tryRelease(int arg) {
        return state.compareAndSet(1, 0);
    }

}
